<template>
  <view class="page">
    <!-- 表单组件 -->
    <mes-form
      :mesForm="jurisdiction !== 'details' ? mesFormTop : detailForm"
    ></mes-form>
    <!-- 备注 -->
    <!-- <mes-form
      :mesForm="jurisdiction !== 'details' ? remark : detailsRemark"
    ></mes-form> -->
    <!-- 间距 -->
    <view class="bottom-height"></view>
    <!-- 重置 -->
    <mes-button
      :type="3"
      :form="buttonFrom"
      v-if="jurisdiction !== 'details'"
    ></mes-button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      index: '', //当前商品索引
      jurisdiction: '', //是否显示按钮权限
      remark: {
        top: 20,
        row: {
          remark: ''
        },
        items: [
          {
            title: '备注',
            slots: {
              field: 'remark',
              type: 'input'
            }
          }
        ]
      },
      detailsRemark: {
        top: 20,
        row: {
          remark: ''
        },
        items: [
          {
            title: '备注',
            slots: {
              field: 'remark'
            }
          }
        ]
      },
      detailForm: {
        top: 20,
        row: {},
        items: [
          {
            title: '商品编号',
            slots: {
              field: 'goods_code'
            }
          },
          {
            title: '商品名称',
            slots: {
              field: 'goods_name'
            }
          },
          {
            title: '商品单位',
            slots: {
              field: 'unit_name'
            }
          },
          {
            title: '数量',
            slots: {
              field: 'number'
            }
          },
          {
            title: '基本单位',
            slots: {
              field: 'default_unit_name'
            }
          },
          {
            title: '基本数量',
            slots: {
              field: 'basic_nmber'
            }
          },
          {
            title: '单价',
            slots: {
              field: 'purchase'
            }
          },
          {
            title: '金额',
            slots: {
              field: 'money'
            }
          }
        ]
      },
      mesFormTop: {
        top: 20,
        row: {},
        items: [
          {
            title: '商品编号',
            required: true,
            slots: {
              field: 'goods_code'
              // icon: 'scan'
            },
            rules: [
              {
                // 是否必要
                type: 'required',
                message: '请选择商品编号'
              }
            ]
          },
          {
            title: '商品名称',
            required: true,
            slots: {
              field: 'goods_name'
              // type: 'input'
            },
            rules: [
              {
                // 是否必要
                type: 'required',
                message: '请输入商品名称'
              }
            ]
          },
          {
            title: '商品单位',
            required: true,
            slots: {
              field: 'unit_name',
              icon: 'arrow',
              type: 'select',
              select: {
                placeholder: '请输入关键词',
                options: [],
                changeEvent: (item) => {
                  this.mesFormTop.row.goods_unit = item.id
                  this.mesFormTop.row.unit_name = item.name
                  this.formatInfo('goods_unit')
                }
              }
            },
            rules: [
              {
                // 是否必要
                type: 'required',
                message: '请选择商品单位'
              }
            ]
          },
          {
            title: '数量',
            required: true,
            slots: {
              // icon: 'yield',
              field: 'number',
              type: 'serial',
              getSerial: (serial) => {
                console.log(serial, '数量')
                this.mesFormTop.row.serial = serial
                this.mesFormTop.row.number = serial.length
                this.formatInfo('number')
              },
              inputEvent: () => {
                this.formatInfo('number')
              }
            },
            rules: [
              {
                // 是否必要
                type: 'required',
                message: '请输入数量'
              }
            ]
          },
          {
            title: '基本单位',
            slots: {
              field: 'default_unit_name'
              // type: 'input'
            }
          },
          {
            title: '基本数量',
            slots: {
              field: 'basic_nmber',
              type: 'input',
              inputEvent: () => {
                this.formatInfo('basic_nmber')
              }
            }
          },
          {
            title: '单价',
            required: true,
            slots: {
              field: 'purchase',
              type: 'input',
              input_type: 'digit',
              inputEvent: () => {
                this.formatInfo('purchase')
              }
            },
            rules: [
              {
                // 是否必要
                type: 'required',
                message: '请输入单价'
              }
            ]
          },
          {
            title: '金额',
            required: true,
            slots: {
              field: 'money',
              type: 'input',
              input_type: 'digit',
              inputEvent: () => {
                this.formatInfo('money')
              }
            },
            rules: [
              {
                // 是否必要
                type: 'required',
                message: '请输入金额'
              }
            ]
          }
        ]
      },
      buttonFrom: {
        leftEvent: () => {
          getApp().globalData.goods.list.splice(this.index, 1)
          uni.navigateBack({ delta: 1 })
        },
        leftText: '删除',
        rightEvent: () => {
          getApp().globalData.goods.list[this.index] = Object.assign(
            this.mesFormTop.row,
            this.remark.row
          )
          uni.navigateBack({ delta: 1 })
        },

        rightText: '保存'
      }
    }
  },
  methods: {
    // 获取变动价格
    async formatInfo(now_field) {
      const postForm = {
        // 商品编号
        goods_code: this.mesFormTop.row.goods_code || null,
        // 商品属性ID（无属性请传空）
        goods_attr: '',
        // 商品单位ID
        goods_unit:
          this.mesFormTop.row.goods_unit || this.mesFormTop.row.unit_id|| null,
        // 仓库ID
        store: '',
        // 数量
        number: this.mesFormTop.row.number|| null,
        // 商品单价
        purchase: this.mesFormTop.row.purchase|| null,
        // 商品含税单价
        tax_included: this.mesFormTop.row.purchase|| null,
        // 折扣率
        discount: '',
        // 折扣额
        dis_money: '',
        // 购货金额
        all_money: this.mesFormTop.row.money|| null,
        // 税率
        tax_rate: 0,
        // 税额
        tax_money: '',
        // 价税合计
        money_tax_price: this.mesFormTop.row.money|| null,
        // 当前变动字段
        now_field:now_field?now_field:null
      }
      const res = await this.$api.buy.common.formatInfo(postForm)
      if (res.code == 200) {
        this.mesFormTop.row.number = res.data.number
        this.mesFormTop.row.purchase = res.data.purchase
        this.mesFormTop.row.money = res.data.purchase * res.data.number
        this.mesFormTop.row.basic_nmber = res.data.basic_nmber
      }
    }
  },
  async onLoad(option) {
    let newObj = JSON.parse(option.obj)
    this.jurisdiction = newObj.name //商品是否有编辑删除权限
    this.index = newObj.index
    // this.mesFormTop.items[3].slots.icon =
    //   getApp().globalData.goods.list[this.index].serial == 1 ? 'yield' : ''
    this.jurisdiction == 'details'
      ? (this.detailForm.row = getApp().globalData.goods.list[this.index])
      : (this.mesFormTop.row = getApp().globalData.goods.list[this.index])

    this.jurisdiction == 'details'
      ? (this.detailsRemark.row.remark =
          getApp().globalData.goods.list[this.index].remark)
      : (this.remark.row.remark =
          getApp().globalData.goods.list[this.index].remark)
    if (this.jurisdiction !== 'details') {
      // 获取商品单位
      this.mesFormTop.items[2].slots.select.options = await this.$tools.getUnit(
        this.mesFormTop.row.goods_id || this.mesFormTop.row.id
      )
    }
    this.mesFormTop.row.basic_nmber=this.mesFormTop.row.number
  }
}
</script>

<style lang="less" scoped>
</style>
